<?php

namespace app\common\model;

class Tool
{
    /**
     * 获取curl的路径的返回数据 get
     * @param $url
     * @param string $data
     * @param bool $jsonDecode
     * @return bool|mixed|string
     */
    public static function getCurl($url,$jsonDecode = true)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_REFERER, $url);
//        curl_setopt($ch, CURLOPT_USERAGENT, 'Eduugo Server Nginx 1.1');
        curl_setopt($ch,CURLOPT_TIMEOUT,20);
        $output = curl_exec($ch);

        curl_close($ch);
        if ($jsonDecode) {
            $output = json_decode($output, true);
        }
        return $output;
    }

    /**
     * 获取curl的路径的返回数据 post
     * @param $url
     * @param $postData
     * @param bool $jsonDecode
     * @return bool|mixed|string
     */
    public static function postCurl($url, $postData)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Server Nginx 1.1');
        curl_setopt($ch,CURLOPT_TIMEOUT,10);

        $output = curl_exec($ch);
        curl_close($ch);

        $output = json_decode($output, true);
        return $output;
    }

    /**
     * 远程下载文件
     * @param $url
     * @param $file
     * @return false|int
     */
    public static function fileDownload($url, $saveUrl)
    {
        $url = "http://127.0.0.1:8000/saveImage?imageUrl={$url}&saveUrl={$saveUrl}";
        Tool::getCurl($url);
    }

    /**
     * 获取两个天数之间的天的列表
     * @param $sday
     * @param $eday
     * @return array
     */
    public static function getDayRangeArr($sday,$eday)
    {
        $rs = array();
        $date_from = strtotime($sday);
        $date_to = strtotime($eday);
        for ($i=$date_from; $i<=$date_to; $i+=86400)
        {
            $rs[] = date("Y-m-d", $i);
        }
        return $rs;
    }

    function chineseToArabic($chineseNumber) {
        $chineseNums = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
        $chineseUnits = array('', '十', '百', '千', '万', '亿', '兆');
        $arabicNumber = 0;
        $tempNumber = 0;
        $unitIndex = 0;
        $zeroFlag = false;

        $length = mb_strlen($chineseNumber, 'UTF-8');
        for ($i = $length - 1; $i >= 0; $i--) {
            $char = mb_substr($chineseNumber, $i, 1, 'UTF-8');
            $charIndex = array_search($char, $chineseNums);

            if ($charIndex !== false) {
                if ($zeroFlag) {
                    $zeroFlag = false;
                    continue;
                }

                $tempNumber += $charIndex * pow(10, $unitIndex);

                if ($unitIndex > 0 && $unitIndex % 4 == 0) {
                    $arabicNumber += $tempNumber * pow(10, ($length - $i - 1) / 4);
                    $tempNumber = 0;
                }

                $unitIndex++;
            } elseif ($char == '零') {
                $zeroFlag = true;
            }
        }

        $arabicNumber += $tempNumber;
        return $arabicNumber;
    }




}